import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:path_provider/path_provider.dart';
import 'package:player/player.dart';
import 'package:player/video_view.dart';

import 'favorite_gesture.dart';

class PlayerPage extends StatefulWidget {
  final String path =
      'https://sample-videos.com/video123/flv/240/big_buck_bunny_240p_10mb.flv';
  final String url;

  const PlayerPage({required this.url});

  @override
  // ignore: library_private_types_in_public_api
  _PlayerPageState createState() => _PlayerPageState();
}

class _PlayerPageState extends State<PlayerPage> {
  String version = 'null';

  @override
  void initState() {
    super.initState();
    Player().getPlatformVersion().then((value) {
      setState(() => version = value!);
    });
  }

  @override
  Widget build(BuildContext context) {
//    var player = FijkPlayer()..setDataSource(widget.path, autoPlay: true);
//    return Scaffold(body: player,);

    var player = Player();
    player.setCommonDataSource(widget.url,
        type: SourceType.net, autoPlay: true);
    print('video-widget>url${widget.url}');
    return FavoriteGesture(child: GestureDetector(
      onLongPress: _onLongPress,
      child: VideoView(
        player: player,
      ),
    ));
  }

//  @override
//  Widget build(BuildContext context) {
//    return Text('当前版本号：$version');
//  }

  // 长按视频弹出对话框
  void _onLongPress() {
    showDialog(
        context: context,
        builder: (context) {
          return AlertDialog(
            title: const Text('提示'),
            content: const Text('点击下载视频？'),
            actions: <Widget>[
              TextButton(onPressed: _saveVideo, child: const Text('确定')),
              TextButton(onPressed: () {Navigator.pop(context, 'cancer');}, child: const Text('取消')),
            ],
          );
        });
  }

  void _saveVideo() async{
    print('save video ');
    const urlPath = '';
    Uri uri = Uri.parse(urlPath);
    String name = uri.pathSegments.last;
    var savePath = await getExternalStorageDirectory();
    Dio().download(urlPath, '${savePath?.path}/$name', onReceiveProgress: (int count, int total){});
  }


}
